package com.eshipenet.shipnet.equipmentextbuilder

import scala.util.Try

sealed trait EquipmentModelAction {
  val id: String
}

case class EquipmentModelInsert(id: String, after: EquipmentModel)                         extends EquipmentModelAction
case class EquipmentModelUpdate(id: String, before: EquipmentModel, after: EquipmentModel) extends EquipmentModelAction
case class EquipmentModelDelete(id: String, before: EquipmentModel)                        extends EquipmentModelAction

object EquipmentModelAction {

  def apply(act: DebeziumParser.Action): Try[EquipmentModelAction] = act match {
    case DebeziumParser.Insert(after, _) =>
      EquipmentModel(after).map(e => EquipmentModelInsert(e.id, e))
    case DebeziumParser.Update(before, after, _) =>
      Try {
        val mBefore = EquipmentModel(before).get
        val mAfter  = EquipmentModel(after).get
        EquipmentModelUpdate(mAfter.id, mBefore, mAfter)
      }
    case DebeziumParser.Delete(before, _) =>
      EquipmentModel(before).map(e => EquipmentModelDelete(e.id, e))
  }

}
